package com.student.controller;


import com.student.entity.Role;
import com.student.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by dongge on 16/9/16.
 */
@Controller
public class RoleController {

    private final static String RLIST = "redirect:/roles";
    private final static String LIST = "role/list";
    private final static String INPUT = "role/input";

    @Autowired
    private ManageService manageService;

    @RequestMapping("/roles")
    public String list(Map<String,Object> map){
        map.put("roles",manageService.getAll(Role.class));
        return LIST;
    }

    @RequestMapping(value = "/role",method = RequestMethod.GET)
    public String input(Map<String,Object> map){
        map.put("role",new Role());
        map.put("roles",manageService.getAll(Role.class));
        return INPUT;
    }

    @RequestMapping(value = "/role",method = RequestMethod.POST)
    public String create(Role role){
        manageService.save(Role.class, role);
        return RLIST;
    }

    @RequestMapping(value = "/role/{id}",method = RequestMethod.GET)
    public String edit(@PathVariable("id")Integer id, Map<String,Object> map){
        map.put("role",manageService.get(Role.class,id));
        map.put("roles", manageService.getAll(Role.class));
        return INPUT;
    }

    /**
     * 有问题还要改才行??????
     * @param id
     * @param parentId
     * @param map
     */
    @ModelAttribute
    public void getRole(@RequestParam(value = "id",required = false)Integer id,
                        @RequestParam(value = "parent.id",required = false)Integer parentId,
                        Map<String,Object> map){
        if(id !=null){
            Role role = (Role)manageService.get(Role.class,id);
            map.put("role",role);
        }
    }

    @RequestMapping(value = "/role",method = RequestMethod.PUT)
    public String update(Role role){
        manageService.update(Role.class,role);
        return RLIST;
    }

    @RequestMapping(value = "/role/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("id")Integer id){
        manageService.delete(Role.class,id);
        return RLIST;
    }
}
